import time

import requests

from sql.sql_conn import Mysql_data
from sql.sql_state import select_gun, update_duiwu, insert_duiwu


class Ss_pp(Mysql_data):
    def ss_comm(self):

        t2 = time.time()
        headers1 = {
            # "Content-Type": "application/json;charset=utf-8",
            #
            #     'User-Agent': random.choice(USER_AGENT_LIST)
            'Accept': '*/*',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh,zh-CN;q=0.9',
            'Cookie': 'WEBSESSID=rdu68qkmgihiek2uaaricr8sm0; p0001_lang=zh_CN; p0001_token=dW5kZWZpbmVk; p0001_acc=%7B%22previewFlag%22%3Atrue%2C%22isDemo%22%3Afalse%2C%22spread%22%3A%22c%22%2C%22oddsType%22%3A%22hk%22%2C%22showDemo%22%3Afalse%2C%22showLogo%22%3Atrue%2C%22selectedMarket%22%3Anull%7D',
            'Referer': 'https://dywu.sp5178.com/whg/preview?langs=2',
            'Sec-Ch-Ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
            'Sec-Ch-Ua-Mobile': '?0',
            'Sec-Ch-Ua-Platform': '"Windows"',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'Token': 'QVI=',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest',
        }
        url = "https://dywu.sp5178.com/whg/sport-odds-preview/3?sprtid=N1&lang=2&spread=c&odd=hk&page=1&sorttyp=1&date=0&plytypgrp=1&gameMenuId=0"
        req = requests.get(url, headers=headers1, timeout=2)
        try:
            req_data = req.json()
            # print(req_data)
            req.close()
            return req_data
        except Exception as e:
            # print(req.text)
            print(e)
        # finally:
        #     print(time.time()-t2)

    def ss_gunqiu(self):
        data = self.ss_comm()
        events = data['events']
        fb_getduiwu = select_gun("game_id", 'bisai_get_ss')  # bisai_get_bb
        fb_get_duiwu = self.run_sql(fb_getduiwu)
        fb_get_duiwu = [i[0] for i in fb_get_duiwu]

        fbduiwu = select_gun("FB_game_id", 'bisai_fb_bb_duiwu')  # bisai_fb_bb_duiwu
        fb_duiwu = self.run_sql(fbduiwu)
        fb_duiwu = [i[0] for i in fb_duiwu]
        ass = []
        for eve in events:
            for eve_data in eve:
                eveid = eve_data['eveid']  # 比赛ID
                dt = eve_data['dt']  # 开赛时间
                lgecapt = eve_data['lgecapt']  # 联赛名称
                tacapt = eve_data['tacapt']  # 主队
                tbcapt = eve_data['tbcapt']  # 客队
                if "角球" in tbcapt:
                    continue
                try:
                    evetm = eve_data["rbinfo"]["evetm"]  # 时间
                except:
                    print(eve_data)

                rbinfo_scoa_scob = str(eve_data["rbinfo"]["scoa"]) + ":" + str(eve_data["rbinfo"]["scob"])  # 得分
                data_all = dt, lgecapt, tacapt, tbcapt, rbinfo_scoa_scob, evetm, eveid
                if int(eveid) not in fb_get_duiwu:
                    if int(eveid) not in fb_duiwu:
                        ass.append(data_all)
                        print('SS',data_all)

                if int(eveid) in fb_get_duiwu:
                    tal = 'info_score', 'current_game_time'
                    data_tal = rbinfo_scoa_scob,evetm
                    for k, v in zip(tal, data_tal):
                        up = update_duiwu("bisai_get_fb", k, "game_id")
                        self.run_sql(up, (v, eveid))
        if ass != []:
            ins = insert_duiwu("bisai_get_ss")
            self.run_sql(ins, ass)
            # print(ins)
        print("SS完成")
if __name__ == '__main__':
    ad = Ss_pp()
    ad.ss_gunqiu()